﻿using MediatR;
using SmartMedicalCare.API.Write.Applications.Commands.轮播图;
using SmartMedicalCare.Domain;
using SmartMedicalCare.ErronCode;
using SmartMedicalCare.Infrastructure.Implaction;

namespace SmartMedicalCare.API.Write.Applications.CommandHandlers.轮播图
{
    public class UpdateCarouselCommandHandler : IRequestHandler<UpdateCarouselCommand, APIResult<int>>
    {
        private readonly IBaseRespository<CarouselModel> carRep;

        public UpdateCarouselCommandHandler(IBaseRespository<CarouselModel> carRep)
        {
            this.carRep = carRep;
        }
        public async Task<APIResult<int>> Handle(UpdateCarouselCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result=new APIResult<int>();
            result.code=ResultCode.修改成功;
            result.Message=ResultCode.修改成功.ToString();
            
            var res=carRep.GetAll().FirstOrDefault(x=>x.CarouselId==request.CarouselId);
            res.Title=request.Title;
            res.ImageUrl=request.ImageUrl;
            res.JumpUrl=request.JumpUrl;
            res.StartTime = request.StartTime;
            res.EndTime= request.EndTime;
            res.Status = request.Status;
            res.Counts=request.Counts;
            

            await carRep.UpdateAsync(res);


            return await Task.FromResult(result);
        }
    }
}
